package sdmakerz_v1.coresyntax.generator;

import com.google.common.collect.Iterables;
import fr.lip6.move.care.CareFactory;
import interfacegen.java.JavaInterfaceFactory;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.xtext.generator.IFileSystemAccess;
import org.eclipse.xtext.generator.IGenerator;
import org.eclipse.xtext.xbase.lib.Exceptions;
import sdmakerz_v1.coresyntax.SDMakerV1RuntimeModule;
import sdmakerz_v1.coresyntax.generator.interfaces.IContext;
import sdmakerz_v1.coresyntax.generator.interfaces.IInputData;
import sdmakerz_v1.coresyntax.generator.interfaces.ISDMakerFolderLevelGen;
import sdmakerz_v1.coresyntax.generator.interfaces.ISDMakerGenerator;
import sdmakerz_v1.coresyntax.generator.interfaces.ISDMakerModelLevelGen;
import sdmakerz_v1.coresyntax.io.SDMakerIO;

@SuppressWarnings("all")
public class SDMakerV1Generator implements IGenerator {
  private List<ISDMakerGenerator> generators;
  
  public SDMakerV1Generator() {
    ArrayList<ISDMakerGenerator> _arrayList = new ArrayList<ISDMakerGenerator>();
    this.generators = _arrayList;
  }
  
  public void addGenerator(final ISDMakerGenerator generator) {
    this.generators.add(generator);
  }
  
  public void addAllGenerator(final List<ISDMakerGenerator> generators) {
    this.generators.addAll(generators);
  }
  
  public void doGenerate(final Resource input, final IFileSystemAccess fsa) {
    try {
      SDMakerIO _sDMakerIO = new SDMakerIO();
      SDMakerIO sdmakerIO = _sDMakerIO;
      List<IInputData> inputs = sdmakerIO.allInputs();
      Class<? extends IContext> _bindIContext = SDMakerV1RuntimeModule.bindIContext();
      IContext context = _bindIContext.newInstance();
      context.init();
      ISDMakerGenerator _newJavaInterfaceGen = JavaInterfaceFactory.newJavaInterfaceGen();
      this.addGenerator(_newJavaInterfaceGen);
      ISDMakerGenerator _newCareGen = CareFactory.newCareGen();
      this.addGenerator(_newCareGen);
      Iterable<ISDMakerFolderLevelGen> _filter = Iterables.<ISDMakerFolderLevelGen>filter(this.generators, ISDMakerFolderLevelGen.class);
      for (final ISDMakerFolderLevelGen generator : _filter) {
        {
          generator.doGenerate(context, fsa);
          context.cleanContext();
        }
      }
      for (final IInputData inputDTmp : inputs) {
        Iterable<ISDMakerModelLevelGen> _filter_1 = Iterables.<ISDMakerModelLevelGen>filter(this.generators, ISDMakerModelLevelGen.class);
        for (final ISDMakerModelLevelGen generator_1 : _filter_1) {
          Resource _input = inputDTmp.getInput();
          boolean _canHandle = generator_1.canHandle(_input);
          if (_canHandle) {
            context.setInput(inputDTmp);
            generator_1.doGenerate(context, fsa);
            context.cleanContext();
          }
        }
      }
    } catch (Exception _e) {
      throw Exceptions.sneakyThrow(_e);
    }
  }
}
